package cn.bird.storm.validated.paramcheck.tool;

import cn.bird.storm.validated.paramcheck.CheckAnnotation;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ReflectUtil;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.List;

@Data
public class CollCheckCondition {
    /**
     * 1、至少一个为空的分组字段
     */
    private String group;
    /**
     * 字段名称
     */
    private String fieldName;
    /**
     * 实际值
     */
    private Object object;
    /**
     * 字符串校验范围
     */
    private List<String> stringScope;
    /**
     * 第一个预留字段
     * 1、自定义校验的 key
     */
    private String reserve1;


    public static CollCheckCondition fromBy(CheckAnnotation checkAnnotation, Object o, Field field){
        CollCheckCondition condition = new CollCheckCondition();
        condition.setGroup(checkAnnotation.group());
        condition.setFieldName(checkAnnotation.fieldName());
        condition.setObject(ReflectUtil.getFieldValue(o, field));
        condition.setReserve1(checkAnnotation.reserve1());
        condition.setStringScope(ListUtil.toList(checkAnnotation.stringScope()));
        return condition;
    }

    public static CollCheckCondition fromByOne(CheckAnnotation checkAnnotation, Object value) {
        CollCheckCondition condition = new CollCheckCondition();
        condition.setGroup(checkAnnotation.group());
        condition.setFieldName(checkAnnotation.fieldName());
        condition.setObject(value);
        condition.setReserve1(checkAnnotation.reserve1());
        condition.setStringScope(ListUtil.toList(checkAnnotation.stringScope()));
        return condition;
    }

}
